'''

@author: wyndem
@Emil:   wyndem@qq.com
@FileName: sqlLiteUtil.py
@Time: 2019/2/22 10:23
@Software: PyCharm
@Description: 

'''
import os

from peewee import *
db = SqliteDatabase('qq.db')


class User(Model):
    name=CharField()
    ac=CharField()
    pwd=CharField()
    phone=CharField()
    integral=IntegerField()

    class Meta:
        database = db

class Card(Model):
    key = CharField()
    value=IntegerField()
    createDate= DateField()
    available = BooleanField()
    class Meta:
        database = db

class Notice(Model):
    title = CharField()
    message=CharField()
    class Meta:
        database = db

class Recording(Model):
    """
        记录表
    """
    card = CharField()
    ac=CharField()
    phone=CharField()
    type=CharField() # 自动 手动
    value = IntegerField()  # 金额
    createDate = DateField() # 充值时间
    class Meta:
        database = db


class Version(Model):
    #版本号
    code = CharField()
    url=CharField()
    #说明
    description= CharField()
    class Meta:
        database = db


async def createDB(app):
    if not os.path.exists("qq.db"):
        with db:
            db.create_tables([User,Notice,Card,Version,Recording])




def getUserInfo(ac):
    try:
        u = User.get(User.ac == ac)
        return u
    except:
        return None




# if __name__ == '__main__':
#     print(getUserToken('mn,mn').token)

#
#
# def createDB():
#     if not os.path.exists("qq.db"):
#         conn = getConn()
#         cursor = conn.cursor()
#         cursor.execute('create table user (name varchar(20) primary key, ac varchar(20),pwd varchar(20),tok varchar(225))')
#         cursor.close()
#         conn.commit()
#         conn.close()
#
# def getConn():
#     return sqlite3.connect('qq.db')
#
#
# def addUser(name1,ac,pwd,token):
#     conn=getConn()
#     cursor = conn.cursor()
#     cursor.execute('insert into user (name, ac,pwd,tok) values (%s,%s,%s,%s)'%(name1,ac,pwd,token))
#     cursor.close()
#     conn.commit()
#     conn.close()
#
# createDB()